﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MvcControlsToolkit
{
    public class Bar : Plot
    {
        /// <summary>
        /// User’s data
        /// </summary>
        public List<List<object>> Data { get; set; }

        public Bar(string name): base(name)
        {
            Name = name;
            Data = new List<List<object>>();

            var first = new List<object>();
            first.Add(2);
            first.Add(6);
            first.Add(7);
            first.Add(10);

            var second = new List<object>();
            second.Add(5);
            second.Add(3);
            second.Add(null);
            second.Add(-7);

            Data.Add(first);
            Data.Add(second);

            SetPlotOptions(
                new PlotOptions
                    { 
                        Type = PlotType.Bar, 
                        Shadow = true 
                    });
        }

        /// <summary>
        /// Default options that will be applied to grid. 
        /// A Grid is object representing the grid on which the plot is drawn
        /// </summary>
        /// <param name="options"></param>
        /// <returns></returns>
        public Bar SetGridOptions(GridOptions options)
        {
            this.Grid = options.ToString();
            return this;
        }

        /// <summary>
        /// Default options that will be applied to all series
        /// </summary>
        /// <param name="options"></param>
        /// <returns></returns>
        public Bar SetPlotOptions(PlotOptions options)
        {
            options.Type = PlotType.Bar;
            this.Options = options.ToString();
            return this;
        }

        public Bar SetLegendOptions(LegendOptions legend)
        {
            this.Legend = legend.ToString();
            return this;
        }

        /// <summary>
        /// Default options that will be applied to all axes
        /// </summary>
        /// <param name="axes"></param>
        /// <returns></returns>
        public Bar SetAxesOptions(AxesOptions axes)
        {
            this.Axes = axes.ToString();
            return this;
        }

        protected override string GetData()
        {
            var result = new StringBuilder();
            foreach (var series in Data)
            {
                result.Append("[");
                foreach (var serie in series)
                {
                    result.Append(String.Format("{0},", serie ?? "null"));
                }
                result.Append("], ");
            }

            if (result.Length > 0)
                result.Remove(result.Length - 2, 1);

            return result.ToString();
        }
    }
}
